package cn.damai.mine.lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ReentrantLockTest {
	
	/**
	 * 死锁
	 */
	
	private static Lock lock = new ReentrantLock();

    private static Lock lock2 = new ReentrantLock();

    public static void main(String[] args) {
        new Thread(() -> {
            lock.lock();
            System.out.println(" get lock of lock ... successed ");
            try {
                Thread.sleep(10 * 1000);

                System.out.println("get lock of lock2... ");

                lock2.lock();

                System.out.println("get lock of lock2... successed");

                System.out.println(Thread.currentThread().getName() + " will sleep 1 minutes");

                Thread.sleep(1 * 60 * 1000);

                System.out.println(Thread.currentThread().getName() + " will unlock of lock2");
                lock2.unlock();

            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }

        }).start();

        new Thread(() -> {
            lock2.lock();
            System.out.println(" get lock of lock2 ... successed ");
            try {
                System.out.println(" try get 'lock'... ");
                lock.lock();

                System.out.println("get lock of lock... successed");

                System.out.println(Thread.currentThread().getName() + " will sleep 1 minutes");

                Thread.sleep(1 * 60 * 1000);

                lock2.unlock();

            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock2.unlock();
            }

        }).start();
    }

}
